package br.ucb.servicos;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import br.ucb.connection.FactoryConnection;
import br.ucb.dao.DaoBanco;
import br.ucb.janela.Cadastro;
import br.ucb.janela.DiologFistConnect;
import br.ucb.visao.Visao;

public class Servicos {

	public static void VerificaCriacaoDeBanco() {
		verificaBancoAcesso();
	}

	public static void verificaBancoAcesso() {
		Connection con = null;
		try {
			con = FactoryConnection.getInstance().criaConexaoFirst();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			DiologFistConnect dfc = new DiologFistConnect();
			dfc.setVisible(true);
		}
		if(con != null){
			verificaBanco();
		}
	}
	public static void verificaBanco() {
		try {
			FactoryConnection.getInstance().criaConexao();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			if(Visao.yes_no("Banco de dados do sistema não encontrado \n  Deseja Criar um novo banco?","AVISO")){
				criaBanco();
				return;
			}
		}
		Visao.showMessage("Verifiricação Completa Banco já existente","Aviso");
	}
	public static void criaBanco(){
		FileReader fr;
		BufferedReader br;
		StringBuffer sb = new StringBuffer();
		try {
			fr = new FileReader("banco.sql");
			br = new BufferedReader(fr);
			while(br.ready())
				sb.append(br.readLine());
			String[] sql = sb.toString().split(";");
			br.close();
			DaoBanco.criaBanco(sql);
			Cadastro cadastro = new Cadastro();
			cadastro.setVisible(true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Visao.showMessage("Banco de Dados Criado Com Sucesso","Aviso");
	}

	public static void salvaSenhaBanco(String user, String senha) throws IOException {
		Properties prop = new Properties();
		try {
			InputStream leitura = new FileInputStream("conexao.properties");
			prop.load(leitura);
			leitura.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// seta o novo cadastro
		prop.setProperty("USUARIO",user);
		prop.setProperty("SENHA",senha);
		FileOutputStream arquivo = new FileOutputStream("conexao.properties");
		// salva no arquivo
		prop.store(arquivo, "Arquivo de Propriedades");
		arquivo.close();
		ResourceBundleUtil.refresh();
	}

	public static String formatDateIn(String data){
		String date[] = data.split("/");
		return date[2]+"-"+date[1]+"-"+date[0];
	}

	public static String formatDateOut(String data){
		String date[] = data.split("-");
		return date[2]+"/"+date[1]+"/"+date[0];
	}
	public static Integer[] parseInteger(String arrayString[]){
		Integer arrayInteger[] = new Integer[arrayString.length];
		for(int i = 0; i < arrayString.length; i++){
			try{
				arrayInteger[i] = Integer.parseInt(arrayString[i]);
			}catch(NumberFormatException ex){
				return null;
			}
		}
		return arrayInteger;
	}
	

}
